package com.qf.controller;

import com.alibaba.fastjson.JSON;
import com.qf.service.CartService;
import com.qf.service.Impl.CartServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;


@WebServlet("/cartAddController.action")
public class CartAddController extends HttpServlet {

    private CartService cartService = new CartServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端传输的数据（商品id，商品的数量）
        String pid = req.getParameter("pid");
        String amount = req.getParameter("amount");
        System.out.println("商品id：" + pid);
        System.out.println("商品的数量：" + amount);

        //获取session对象
        HttpSession session = req.getSession(false);

        //调用Service层方法
        cartService.add(pid,amount,session);

        //创建Map集合
        HashMap<String, Object> map = new HashMap<>();
        map.put("state",200);

        resp.getWriter().write(JSON.toJSONString(map));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
